package com.bdqn.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.bdqn.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;


@ConfigurationProperties(prefix = "alipay")//此配置类可以和alipay配置文件进行绑定
@Component
@Data
public class AlipayTemplate { //也可以在配置文件中进行配置

    //在支付宝创建的应用的id
    private   String app_id = "2021000118605913";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCdO97iWioyTTbGh0mWsG1O9G2oK13J9pxDkDzY00J7OkA5mIcDaTzO1YP95eYdRGNwO61VUn2M6zN6PPfcXQNPajB1cJZmn6JtBOR2IsEm6dHYMNHpfHV4GCahQhfQXYsMwIIzxSYAWUdZWhNq6KSnbEMIocb9UiPR/R54BNWZmpMLNB7KPi9kq5IlID/IHDQL75pjdRZW8o4xBz1ZaYB1lpSSKeGpH0d9lPn62wSB8Mde9P75biQy53bkUYk28T86C16hLDQ7dJj0F/ymPBkc43FJgipmhgRCmZjlPa51MOJwb4Y6S6Kl5odNQj56kDiuR2VXSe9gFudauvVOj1VvAgMBAAECggEADv1uOlYuP61s03CCa4NWaHiVG7tGrcJ9E1Pk+Sbq56SeCt+nv4OA8JcahlIspg2nDUQjOkcrAQxLVBQL7U5QynltNUUOkaoWah5VSY32rCcOgnXlDUV4TYK/7A/JBWxlnUmdICJjhRT94F7eCwsy52NC4MfLmMsD7zkadoa/dm1HzKOoKocJQGcfteWmdXf0soQangofB0N2gTYqJpo88LB+H0Ut0Lu4XraBN5QaN0FexrAA8zd9xaW7JoRN1TQBSObHXyFRGwChIJ6hD7/TPGVOvrikH2ezhgEeuxbtDDIUsy/vu4tH/ZBtvhLEVFInuSTiitla5OtT/oWpjwGwwQKBgQDXsX61dyMkXZTH9Fks+PeLKOxyYo8rgnbQ+rs6idi2qYV2of2I2BkAqv+Izmxn5nMvOilTq3Q8SZc/p368FQRgByCiGM2AdqpuvTEYzIEi6LzlhLfPdbXcupLz6FRJbCwaQew8krYanpchI2aZIF+lM64mrUNLT7owWgp9tUINTQKBgQC6nb8MTuCK4wRt4JQWnWKFtmaXsLl1bSEIvScpS0nXcbQTIYiS64MeE9vrMCXFHXlp7Hdxe4UsiSD7aB0Hvof54FACw6u41hdaWQBjbN6RZptvve6fciGnIjjt6Vcyba8xgzga93CstLDxiJEj6iY/wjHXbD4hBUpSTAMQz5S/qwKBgQCxRAGz1579Ex/kmW/0owSmzNJOS6wIgSdvJrFbMdD2hinWztkUby1VPDPY1nWHioDdrnoqdn2H0p9IRDrHDqrS1Oi/74xs6Jm3eytvixsSep5MLx/1k3MMXhXJ+QXUdd9lEQq62YRqKi5goLyAWnfgyLF2vezRZ0vI0ydOGvqW/QKBgHTurSxgLFC6iSb0RraY7YgNT+GSydKri7x3EZfNZjqKM6SdKX+t9N2e4flPlQsbOd3GXt2L4VmTn0gI58/Xckp/ZIMm6m4KMF3bUOP77sY6/gCZiXD9Sdr5FaT1xj6B/fnPWBOFomOFYS4nPZK8gDUJUMPEPnXgY76HhJA/Edy7AoGAVp1tkOuADo/NJSe5f1WjnX+aqDFEJoWRnuT68b+i2mqsDB+xhDVxUZKLeaLbEfhtllkhx48JxDDBYdqr3VkURogR2rW0qMcFBeUaXac66aJ7dAriojyP55W62nBaMHVB5bLUqGh8jelFB7TMJKWX9aCDG1XIRT2G6T17sKZ0EIY=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn6C8bj3r2MDI5MEJCX4HAxpEc7ild7cihw8gXY3BCpGHg9TruZTnUfNjz2jU/Raoc5S0xyATjXeZ2whTY45s/bwu1Y3uJPg0d01NpUHGuBhYmp5Cal/SighhWU5Piiqzbfz3CUexXEzG2u/LWWYh/zDcrzeeXOlDvcmEiVcN+UAsc8gP2h6JAZHkwMTdefeWwNKsxzSvDy6Koqs4hFn88nm7/B3weIyXzozf+SrO8Up98BCg5AQrL5PPBBbv3Ma/CmezNGzaFwlS6fMV2POl5kr0qlkHtpPvRwBQOE8MWwlIaJOujq2TlmVt2m2V2DiVsgwu0fFSBkRO8OnByW3HvwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
                                        //内网穿透地址
    private  String notify_url = "http://498j78185w.zicp.vip/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.yunmall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
